package weibo4android.androidexamples;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import cn.com.karl.logic.MainService;
import cn.com.karl.net.NetUtil;
import weibo4android.User;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.androidexamples.R;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
    /** Called when the activity is first created. */
	private TabHost tabHost;
	private TextView textUserName;
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			 
			 // textUserName.setText(msg.obj.toString());
		};
	};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
				WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        setContentView(R.layout.main);
         //textUserName=(TextView) findViewById(R.id.head_userName);
         
        if(getIntent().getBooleanExtra("isHave", false)==true){
        	/* SharedPreferences preferences=getSharedPreferences("ss", Context.MODE_PRIVATE);
			   String userName=preferences.getString("userName", "");
			   textUserName.setText(userName);
			   Log.e("------------------", userName);*/
        }else{
        Uri uri=this.getIntent().getData();
		try {
			RequestToken requestToken= OAuthConstant.getInstance().getRequestToken();
			final AccessToken accessToken=requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
			OAuthConstant.getInstance().setAccessToken(accessToken);
			
			new Thread(){
				@Override
				public void run() {
					 Weibo weibo=OAuthConstant.getInstance().getWeibo();
						//weibo.setToken(OAuthConstant.getInstance().getToken(), OAuthConstant.getInstance().getTokenSecret());
					   weibo.setToken( accessToken.getToken(),accessToken.getTokenSecret());
					   User user = null;
					   try {
						 user=weibo.showUser(String.valueOf(accessToken.getUserId()));
						 Message msg=handler.obtainMessage();
						 msg.obj=user.getName();
						 Log.e("-----------------------------------------", user.getName());
						 handler.sendMessage(msg);
					} catch (WeiboException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// TODO Auto-generated method stub
					SharedPreferences preferences=getSharedPreferences("ss",Context.MODE_PRIVATE);
					Editor editor=preferences.edit();
					editor.putString("userId",String.valueOf(accessToken.getUserId()));
					editor.putString("token", accessToken.getToken());
					editor.putString("tokenSecret", accessToken.getTokenSecret());
					editor.putString("userName", user.getName());
					editor.putString("iconUrlString", user.getProfileImageURL().toString());
					editor.commit();
				}
			}.start();
			
		} catch (WeiboException e) {
			e.printStackTrace();
		}
       }
        
        tabHost=this.getTabHost();
        TabHost.TabSpec spec;
        Intent intent;
        
        intent=new Intent().setClass(this, HomeActivity.class);
        spec=tabHost.newTabSpec("首页").setIndicator("首页").setContent(intent);
        tabHost.addTab(spec);
        
        intent=new Intent().setClass(this,MsgActivity.class);
        spec=tabHost.newTabSpec("信息").setIndicator("信息").setContent(intent);
        tabHost.addTab(spec);
        
        intent=new Intent().setClass(this, MyContactActivity.class);
        spec=tabHost.newTabSpec("资料").setIndicator("资料").setContent(intent);
        tabHost.addTab(spec);
        
        intent=new Intent().setClass(this,SearchActivity.class);
        spec=tabHost.newTabSpec("搜索").setIndicator("搜索").setContent(intent);
        tabHost.addTab(spec);
     
        intent=new Intent().setClass(this, MoreActivity.class);
        spec=tabHost.newTabSpec("更多").setIndicator("更多").setContent(intent);
        tabHost.addTab(spec);
        
        RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_radio);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.radio_button0://首页
					tabHost.setCurrentTabByTag("首页");
					break;
				case R.id.radio_button1://信息
					tabHost.setCurrentTabByTag("信息");
					break;
				case R.id.radio_button2://资料
					tabHost.setCurrentTabByTag("资料");
					break;
				case R.id.radio_button3://搜索
					tabHost.setCurrentTabByTag("搜索");
					break;
				case R.id.radio_button4://更多
					tabHost.setCurrentTabByTag("更多");
					break;
				default:
					tabHost.setCurrentTabByTag("首页");
					break;
				}
			}
		});
    }
	
    
}